﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace AutoUpgradeManager.ControlForm
{
    /// <summary>
    /// Window_FolderBrowser.xaml 的交互逻辑
    /// </summary>
    public partial class Window_FolderBrowser : Window
    {
        public Window_FolderBrowser()
        {
            InitializeComponent();

            foreach (System.IO.DriveInfo driver in DriveInfo.GetDrives())
            {
                TreeViewItem t = new TreeViewItem();
                t.Tag = driver.RootDirectory;
                t.Header = NewTreeViewItem(driver.ToString());
                try
                {
                    if (driver.RootDirectory.EnumerateDirectories().Count() > 0) t.Items.Add("*");
                }
                catch { }

                tView.Items.Add(t);
            }

        }
        /// <summary>
        /// 新的树节点对象
        /// </summary>
        /// <param name="dirString"></param>
        /// <returns></returns>
        private DockPanel NewTreeViewItem(string dirString)
        {
            DockPanel dp = new DockPanel();
            dp.LastChildFill = true;

            Image image = new Image();

            image.Source = new BitmapImage(new Uri("pack://application:,,,/AutoUpgradeManager;component/Image/folder.png"));
            image.Width = 16;
            image.Height = 16;

            TextBlock txtblock = new TextBlock();
            txtblock.Text = dirString;

            dp.Children.Add(image);
            dp.Children.Add(txtblock);

            return dp;

        }
        /// <summary>
        /// 改变Image图片
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="IsOpen"></param>
        private void ChangeImage(DockPanel panel,  bool IsOpen)
        {
            foreach (UIElement ui in panel.Children)
            {
                if (ui is Image)
                {
                    ((Image)ui).Source = new BitmapImage(new Uri(string.Format("pack://application:,,,/AutoUpgradeManager;component/Image/{0}.png", IsOpen ? "folder_open" : "folder")));
                    break;
                }
            }


        }

        /// <summary>
        /// 文件夹路径
        /// </summary>
        public string SelectedPath
        {
            get;
            private set;
        }
        /// <summary>
        /// 树展开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tView_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem tItem = (TreeViewItem)e.OriginalSource;

            if (tItem.Header is DockPanel) ChangeImage((DockPanel)tItem.Header, true);

            tItem.Items.Clear();
            DirectoryInfo dir = tItem.Tag as DirectoryInfo;

            if (dir == null) return;

            try
            {
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    TreeViewItem t = new TreeViewItem();
                    t.Tag = d;
                    t.Header = NewTreeViewItem(d.ToString());
                    try
                    {
                        if (d.EnumerateDirectories().Count() > 0) t.Items.Add("*");
                    }
                    catch { }
                    tItem.Items.Add(t);
                }
            }
            catch { }
        }

        /// <summary>
        /// 树折叠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tView_Collapsed(object sender, RoutedEventArgs e)
        {
            TreeViewItem tItem = (TreeViewItem)e.OriginalSource;
            if (tItem.Header is DockPanel) ChangeImage((DockPanel)tItem.Header, false);
        }

        /// <summary>
        /// 关闭浏览器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Close_Click(object sender, RoutedEventArgs e)
        {
            SelectedPath = "";
            this.DialogResult = false;
        }
        /// <summary>
        /// 确认文件夹选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, RoutedEventArgs e)
        {
            SelectedPath = Txt_Dir.Text;
            this.DialogResult = true;
        }

    }
}
